﻿@{
    ViewBag.Title = "RoleBindMenu";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@section Head
{
   <script type="text/javascript" language="javascript">
       $(document).ready(function () {
           var maiheight = document.documentElement.clientHeight;
           var mainWidth = document.documentElement.clientWidth - 3; // 减去边框和左边的宽度
           var gh = maiheight - 213;
           $("#modularid").height(gh);
           var option = {
               height: maiheight - 113,
               width: mainWidth - (mainWidth * 0.3),
               url: '@Url.Action("GetRoleList")', // 参数要放在）括号的外面
               dataType: 'json',
               colModel: [
					{ display: '角色编号', name: 'RoleId', width: 100, sortable: false, hide: false, align: 'left', iskey: true },
                    { display: '角色名称', name: 'RoleName', width: 200, sortable: false, hide: false, align: 'left' },
					{ display: '角色说明', name: 'RoleMeno', width: 200, sortable: false, hide: false, align: 'left' }
				],
               buttons: [
							{ name: 'Add', displayname: "绑定角色菜单", title: "绑定角色菜单", bclass: 'add', onpress: toolbarItemAdd_onclick },
                            { name: 'Delete', displayname: "删除菜单绑定", title: "删除菜单绑定", bclass: 'edit', onpress: toolbarItemDel_onclick },
							{ separator: true }
						 ],
               title: false,
               singleselected: false,
               usepager: true,
               resizable: true,
               page: 1, //默认当前页  
               total: 1, //总页面数  
               useRp: true, //是否可以动态设置每页显示的结果数  
               rp: 10, // 每页默认的结果数  
               rpOptions: [10, 15, 20], //可选择设定的每页结果数
               showcheckbox: true,
               selectedonclick: true,
               rowbinddata: true,
               hideOnSubmit: true, //隐藏提交  
               autoload: true
           };
           $("#allrolelist").flexigrid(option);

           $("#ptree").treeview({
               url: '@Url.Action("GetAllMenuList")',
               showcheck: false,
               onnodeclick: TreeNode_Click,
               theme: "bbit-tree-no-lines" //bbit-tree-lines ,bbit-tree-no-lines,bbit-tree-arrows
           }).height(maiheight - 30);

           function TreeNode_Click(data) {
               if (data) {
                   $("#moId").val(data.id);
               }
           }
           function toolbarItemAdd_onclick(cmd, grid) {
               var meid = $("#moId").val();
               var items = $("#allrolelist").getSelectedRows();
               if ((meid == "") || (items.length == 0)) {
                   showLoadingMsg("请选择要绑定的菜单和角色!", { left: 400, top: 6 }, true, 3000);
                   return false;
               }
               var rowcount = 0;
               for (var i = 0; i < items.length; i++) {
                   //debugger;
                   var roleid = items[i][0];
                   $.ajax({
                       type: "POST",
                       url: '@Url.Action("BindMenuToRole")',
                       data: {roleid:roleid, meid:meid},
                       dataType: "json",
                       async: false,
                       success: function (data) {
                           if (data) {
                               rowcount = rowcount + ((data == -1) ? 0 : data);
                           }
                       },
                       error: function () { }
                   });   // end of ajax                    
               }
               showLoadingMsg("共有" + rowcount.toString() + "个角色绑定到菜单!", { left: 400, top: 6 }, true, 3000);
           } // end of Add_onclick
           function toolbarItemDel_onclick(cmd, grid) {
               var meid = $("#moId").val();
               var items = $("#allrolelist").getSelectedRows();
               if ((meid == "") || (items.length == 0)) {
                   showLoadingMsg("请选择要删除绑定的角色和菜单!", { left: 400, top: 6 }, true, 3000);
                   return false;
               }
               var rowcount = 0;
               for (var i = 0; i < items.length; i++) {
                   var roleid = items[i][0];
                   $.ajax({
                       type: "POST",
                       url: '@Url.Action("NotBindMenuToRole")',
                       data: { roleid: roleid, meid: meid },
                       dataType: "json",
                       async: false,
                       success: function (data) {
                           if (data) {
                               rowcount = rowcount + ((data == -1) ? 0 : data);
                           }
                       },
                       error: function () { }
                   });   // end of ajax  
               }
               showLoadingMsg("共有" + rowcount.toString() + "个角色从菜单中解除绑定!!", { left: 400, top: 6 }, true, 3000);
           } // end of Del_onclick
           function refreshGrid() {
               $("#allrolelist").flexReload();
           }
           function refresh() {
               showLoadingMsg("保存成功", { left: 200, top: 6 }, true, 3000);
               $("#allrolelist").flexReload();
           }
       });                             //end document ready() 
    </script>
}
<div id="container">
    <div id="sidebar">
        <div class="title">菜单列表</div>
        <div id="ptree">
        </div>
    </div>
    <div id="content">
        <div id="title" class="title">角色列表</div>
        <table id="allrolelist" style="display: none;">
        </table>
    </div>
    <input type="hidden" id="moId" />
    <input type="hidden" id="hdDicType" />
</div>



